import axios from 'axios'
import { get_lid } from '@/utils/getPageDecrypt'
import Cookie from "../../cookie";
import Bearer from "../../Bearer";
// import { store } from "~store"
// import jinritemaiDecrypt from "@/utils/jinritemaiDecrypt"
const service = axios.create({
  baseURL: 'https://compass.jinritemai.com',
  timeout: 200000,
})
// http请求拦截

service.interceptors.request.use(config => {
  // config.headers['t'] = Cookie
  config.headers['Authorization'] = `Bearer ${Bearer}`

  if (config.paramete) {
    // const { __token, msToken } = jinritemaiDecrypt

    // config.paramete.__token = __token
    config.paramete._bid = config.paramete._bid || 'ffa_goods'
    config.paramete._lid = get_lid()
    config.paramete.appid = 1
    config.paramete._ = new Date().getTime()
    // config.paramete.msToken = msToken
    if (!/\?/.test(config.url)) {
      let str = splitJoint(config.paramete);
      config.url += str;
      if (config.method == 'GET' || config.method == 'get') {
        config.data = undefined
      }
    }
  }
  // console.log('configurl', config.url)
  return config
}, error => {
  // Do something with request error
  Promise.reject(error)
});
function splitJoint(obj) {
  let arr = [];
  for (let key in obj) {
    arr.push(key + '=' + obj[key]);
  }
  return arr.length > 0 ? '?' + arr.join('&') : '';
}

// response 拦截器
service.interceptors.response.use(response => {
  return response.data;

}, error => {

  return Promise.reject(error)
})
export default service;
